#include "Tiles.h"

Tiles::Tiles(float x, float y, float z, TileType tileType, SpellType spell)
{
	this->isNode = false;
	this->tileType = tileType;

	this->spell = spell;

	setSpell();

	//set random rect
	setRect();

	//set tile pos
	mTilePos = D3DXVECTOR3(x, y, z);

	//set tile center
	mTileCenter = D3DXVECTOR3(32.0f, 32.0f, 0.0f);
	
	

	node = 0;
}

Tiles::Tiles(float x, float y, float z, TileType tileType, SpellType spell, bool coolDown, int nodeID)
{
	this->isNode = true;
	this->tileType = tileType;

	this->spell = spell;

	setSpell();

	//set random rect
	setRect();

	//set tile pos
	mTilePos = D3DXVECTOR3(x, y, z);

	//set tile center
	mTileCenter = D3DXVECTOR3(32.0f, 32.0f, 0.0f);

	//set tile rank
	int Rank = 0;
	
	//this is causing a memory leak
	//not sure why, we must fix this
	
	this->node = new Node(coolDown, nodeID);
}
Tiles::~Tiles()
{
	#pragma message("Freeing Node")
	FreeNode();
}

void Tiles::setSpell()
{
	switch(spell)
	{
		case TrunkTile:
			this->spellTypeCol = 0;
			this->spellTypeRow = 0;
			break;

		case Fire:
			this->spellTypeCol = 4;
			this->spellTypeRow = 0;
			break;

		case Water:
			this->spellTypeCol = 8;
			this->spellTypeRow = 0;
			break;

		case Wind:
			this->spellTypeCol = 12;
			this->spellTypeRow = 0;
			break;

		case Earth:
			this->spellTypeCol = 0;
			this->spellTypeRow = 1;
			break;

		default:
			this->spellTypeCol = 0;
			this->spellTypeRow = 0;
			break;
	}
}

void Tiles::FreeNode()
{
	node = 0;
	delete this->node;
}

void Tiles::setCenter(float x, float y, float z)
{
	mTileCenter = D3DXVECTOR3(x, y, z);
}

void Tiles::setPos(float x, float y, float z)
{
	mTilePos = D3DXVECTOR3(x, y, z);
}

void Tiles::setRect()
{
	//set button press
	setButton();

	R.left = (buttonCol * 64) + (spellTypeCol * 64);
	R.top = 2048 - (((buttonRow+1) * 64) + (tileType * 128) + (spellTypeRow * 640));
	R.right = ((buttonCol + 1) * 64) + (spellTypeCol * 64);
	R.bottom = 2048 - ((buttonRow * 64) + (tileType * 128)+ (spellTypeRow * 640));

}

Node* Tiles::getNode()
{
	return this->node;
}

int Tiles::getButtonRow()
{
	//row
	int lowestRow = 0;
	int highestRow = 1;
	
	int range2 = (highestRow - lowestRow) + 1;

	return lowestRow + int( range2 *rand()/(RAND_MAX + 1.0)); //row
}

int Tiles::getButtonCol()
{
	/*//col
	int lowest = 0; 
	int highest = 3;
	int range = (highest - lowest) + 1;
	return lowest+int(range*rand()/(RAND_MAX + 1.0)); //col*/

	int lowest = 0; 
	int highest = 7;
	int range = (highest - lowest) + 1;
	return lowest+int(range*rand()/(RAND_MAX + 1.0)); //col
}

void Tiles::setButton()
{	
	//using this so i know when to set first two buttons, after that it is useless
	static int counter = 0;
	
	//this will store last button value
	//Button done before buttonValue2
	static int buttonValue = 0;
	//Button most recently populated
	static int buttonValue2 = 0;
	//Holds the value of the last tile before the last node
	static int previousToNode = -1;
	//Holds value of the first tile in a branch
	static int firstList = -1;
	//Holds the count for the last tile before a node
	static int lastCount = 0;
	//Holds the last node populated
	static int lastNode = -1;

	//variable saying to check buttons
	static bool check = false;
	
	//int i = getButtonRow();
	//int j = getButtonCol();
	
	//get a button value
	//int button = ((i * j) - 1);
	int button = getButtonCol();

	//check button
	if(check)
	{
		while(buttonValue == button || (counter - lastCount == 31 && (previousToNode == button || firstList == button)) || (counter - lastCount == 32 && lastNode == button))
		{
			//i = getButtonRow();
			//j = getButtonCol();

			//button = ((i*j) - 1);
			button = getButtonCol();
		}
		if(counter - lastCount == 31)
		{
			previousToNode = -1;
			firstList = -1;
		}
	}

	if(isNode)
	{
		previousToNode = buttonValue2;
		lastCount = counter;
		lastNode = button;
	}
	if(lastCount + 1 == counter)
	{
		firstList = button;
	}


	if(counter == 0)
		buttonValue = button;
	
	if(counter == 1)
	{
		buttonValue2 = button;
		check = true;
	}

	if(counter > 1)
	{
		buttonValue = buttonValue2;
		buttonValue2 = button;
	}

	//set button press
	switch(button)
	{
		case 0:
			this->buttonPress = 0;
			buttonRow = 0;
			buttonCol = 0;
			break;

		case 1:
			this->buttonPress = 1;
			buttonRow = 0;
			buttonCol = 1;
			break;

		case 2:
		this->buttonPress = Up;
					buttonRow = 0;
			buttonCol = 2;
		break;

		case Down:
		this->buttonPress = Down;
					buttonRow = 0;
			buttonCol = 3;
		break;

		case A:
		this->buttonPress = A;
			buttonRow = 1;
			buttonCol = 0;
		break;

		case S:
		this->buttonPress = S;
			buttonRow = 1;
			buttonCol = 1;
		break;

		case D:
		this->buttonPress = D;
					buttonRow = 1;
			buttonCol = 2;
		break;

		case W:
		this->buttonPress = W;
					buttonRow = 1;
			buttonCol = 3;
		break;
	}

	//increment counter
	counter++;
}

int Tiles::getButtonPress()
{
	return buttonPress;
}